package com.hxit.huaxiamelongarden.service.impl;

import com.hxit.huaxiamelongarden.domin.po.ChatMessage;
import com.hxit.huaxiamelongarden.mapper.ChatMessageMapper;
import com.hxit.huaxiamelongarden.service.ChatHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ChatHistoryServiceImpl
 * @description 历史消息逻辑实现类
 * @Author 12694
 * @Date 2025/1/30 20:59
 * @Version 1.0
 **/
@Service
public class ChatHistoryServiceImpl implements ChatHistoryService {
    @Autowired
    private ChatMessageMapper chatMessageMapper;

    private static final Logger logger = LoggerFactory.getLogger(ChatHistoryServiceImpl.class);

    // 查询用户与另一用户的聊天记录
    public List<ChatMessage> getChatHistory(int userId, int friendId) {
        try {
            // 根据传入的 userId 和 friendId 查询聊天记录
            List<ChatMessage> chatHistory = chatMessageMapper.selectMessages(userId, friendId);
            logger.info("成功查询聊天记录，用户ID：{}，好友ID：{}", userId, friendId); // 打印中文日志
            return chatHistory;
        } catch (Exception e) {
            logger.error("查询聊天记录失败，用户ID：{}，好友ID：{}", userId, friendId, e); // 打印中文日志和异常信息
            throw new RuntimeException("查询聊天记录失败，数据库错误");
        }
    }

}

